package router

import (
	"gomain/app/api"
	"gomain/app/middleware"
	"gomain/app/model"
	"strings"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gview"
)

func init() {
	s := g.Server()

	// 静态文件服务
	s.SetIndexFolder(true)
	s.SetServerRoot("public")

	// 模板引擎配置
	s.SetView(gview.New("public/template"))

	// 全局404处理
	s.BindStatusHandler(404, func(r *ghttp.Request) {

		if !strings.Contains(r.GetHeader("Accept"), "text/html") {
			r.Response.WriteJson(g.Map{
				"code": 404,
				"msg":  "请求的资源不存在",
				"data": nil,
			})
		} else {
			r.Response.WriteStatus(200, nil)
			_ = r.Response.WriteTpl("error/404.html")
		}
	})

	s.BindStatusHandler(403, func(r *ghttp.Request) {
		if !strings.Contains(r.GetHeader("Accept"), "text/html") {
			r.Response.WriteJson(g.Map{
				"code": 403,
				"msg":  "没有访问权限",
				"data": nil,
			})
		} else {
			r.Response.WriteStatus(200, nil)
			_ = r.Response.WriteTpl("error/403.html")
		}
	})

	// 全局500处理
	s.BindStatusHandler(500, func(r *ghttp.Request) {
		if !strings.Contains(r.GetHeader("Accept"), "text/html") {
			r.Response.WriteJson(g.Map{
				"code": 500,
				"msg":  "服务器内部错误",
				"data": nil,
			})
		} else {
			r.Response.WriteStatus(200, nil)
			_ = r.Response.WriteTpl("error/500.html")
		}
	})

	// 首页路由
	s.BindHandler("/", func(r *ghttp.Request) {
		// 检查是否已登录
		userVar, _ := r.Session.Get("user")
		if userVar != nil {
			r.Response.RedirectTo("/index")
			return
		}
		r.Response.RedirectTo("/login")
	})

	s.BindHandler("/login", func(r *ghttp.Request) {
		// 如果已登录，直接跳转到首页
		// userVar, _ := r.Session.Get("user")
		// if userVar != nil {
		// 	r.Response.RedirectTo("/index")
		// 	return
		// }
		r.Response.WriteTpl("login.html")
	})

	// 添加首页路由
	s.BindHandler("/index", func(r *ghttp.Request) {
		userVar, _ := r.Session.Get("user")
		if userVar == nil {
			r.Response.RedirectTo("/login")
			return
		}

		// 将 gvar.Var 转换为 *model.User
		var user *model.User
		if err := userVar.Scan(&user); err != nil {
			r.Response.RedirectTo("/login")
			return
		}

		r.Response.WriteTpl("index.html", g.Map{
			"user": g.Map{
				"nickname": user.Nickname,
				"username": user.Username,
				"id":       user.Id,
			},
		})
	})

	// 添加欢迎页路由
	s.BindHandler("/welcome", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("welcome.html")
	})

	// 用户管理页面
	s.BindHandler("/user", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("user/list.html")
	})

	// 用户编辑页面
	s.BindHandler("/user/edit", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("user/edit.html")
	})

	// 用户添加页面
	s.BindHandler("/user/add", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("user/add.html")
	})

	// 用户基本资料页面
	s.BindHandler("/user/profile", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("user/profile.html")
	})

	// API路由
	s.Group("/api", func(group *ghttp.RouterGroup) {
		group.Middleware(middleware.CORS)

		// 需要认证的接口
		group.Group("/", func(group *ghttp.RouterGroup) {
			// 无需认证的接口
			group.Middleware(middleware.Auth)
			group.Middleware(middleware.DynamicPermission)

			// 用户相关接口
			group.Group("/user", func(group *ghttp.RouterGroup) {
				group.ALL("/", new(api.UserApi))
				// 用户角色管理需要admin权限
				group.Middleware(middleware.CheckPermission("admin"))
				group.POST("/roles", new(api.UserApi), "AssignRoles")
				group.POST("/list", new(api.UserApi), "List")
				group.POST("/login", new(api.UserApi), "Login")
				group.GET("/get/:id", new(api.UserApi), "Get")
				group.GET("/profile", new(api.UserApi), "GetProfile")
				group.POST("/updateProfile", new(api.UserApi), "UpdateProfile")
			})

			// 角色管理接口
			group.Group("/role", func(group *ghttp.RouterGroup) {
				group.Middleware(middleware.CheckPermission("admin"))
				group.GET("/", new(api.RoleApi), "List")
				group.GET("/:id", new(api.RoleApi), "Get")
				group.POST("/", new(api.RoleApi), "Add")
				group.PUT("/:id", new(api.RoleApi), "Update")
				group.DELETE("/:id", new(api.RoleApi), "Delete")
				group.GET("/permissions/:id", new(api.RoleApi), "GetPermissions")
				group.POST("/permissions", new(api.RoleApi), "AssignPermissions")
			})

			// 商品分类接口
			group.Group("/category", func(group *ghttp.RouterGroup) {
				group.ALL("/", new(api.CategoryApi))
			})

			// 商品接口
			group.Group("/product", func(group *ghttp.RouterGroup) {
				group.ALL("/", new(api.ProductApi))
			})

			// 订单接口
			group.Group("/order", func(group *ghttp.RouterGroup) {
				group.ALL("/", new(api.OrderApi))
			})

			// 会员接口
			group.Group("/member", func(group *ghttp.RouterGroup) {
				group.ALL("/", new(api.MemberApi))
			})

			// 菜单管理接口
			group.Group("/menu", func(group *ghttp.RouterGroup) {
				group.Middleware(middleware.CheckPermission("admin"))
				group.GET("/", new(api.MenuApi), "List")
				group.GET("/list", new(api.MenuApi), "List2")
				group.GET("/:id", new(api.MenuApi), "Get")
				group.POST("/", new(api.MenuApi), "Add")
				group.PUT("/:id", new(api.MenuApi), "Update")
				group.DELETE("/:id", new(api.MenuApi), "Delete")
				group.GET("/tree", new(api.MenuApi), "GetTree")
				group.GET("/data", new(api.MenuApi), "GetMenuData")
			})

			// 权限管理接口
			group.Group("/permission", func(group *ghttp.RouterGroup) {
				group.Middleware(middleware.CheckPermission("admin"))
				group.ALL("/", new(api.PermissionApi))
			})
		})
	})

	// 菜单管理页面
	s.BindHandler("/menu", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("menu/list.html")
	})
	s.BindHandler("/menu/add", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("menu/edit.html")
	})
	s.BindHandler("/menu/edit", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("menu/edit.html")
	})

	// 角色管理页面
	s.BindHandler("/role", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("role/list.html")
	})
	s.BindHandler("/role/add", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("role/edit.html")
	})
	s.BindHandler("/role/edit", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("role/edit.html")
	})
	s.BindHandler("/role/power", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("role/power.html")
	})

	// 权限管理页面
	s.BindHandler("/permission", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("permission/list.html")
	})
	s.BindHandler("/permission/add", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("permission/edit.html")
	})
	s.BindHandler("/permission/edit", func(r *ghttp.Request) {
		_ = r.Response.WriteTpl("permission/edit.html")
	})
}
